#!/usr/bin/python

__version__ = (0,0,1)
__author__ = "Daniel Berns - daniel.w.berns at gmail.com"
__date__ = "20080611"

import Utils

"""
  Test Utils.py module
"""

def test_walk_matching_filenames(argv):
    """
       Test Utils.listdir_files_matching_filenames(pattern, root):
    """
    if argv == None or len(argv) != 3:
        print 'test_walk_matching_filenames'
        print '  First argument root: dir for walking'
        print '  Second argument pattern: pattern for matching filenames'
        print '  Third argument expected: text file with expected results'
        raise ValueError('test_walk_matching_filenames: three arguments expected')
    root = argv[0]
    pattern = argv[1]
    expected = open(argv[2], 'r')
    auxiliar = expected.readlines()
    expected.close()
    results = []
    for item in auxiliar:
        results.append(item[:-1])
    count = 0
    for item in Utils.walk_matching_filenames(pattern, root):
        if item in results:
            count += 1
        else:
            raise ValueError('unexpected item ---- %s' % item)
    if count != len(results):
       raise ValueError('bad count ---- %d != %d' % (count, len(results)))

def test_listdir_files(argv):
    """
       Test Utils.listdir_files(root):
    """
    if argv == None or len(argv) != 2:
        print 'test_listdir_files'
        print '  First argument root: dir for walking'
        print '  Second argument expected: text file with expected results'
        raise ValueError('test_listdir_files: two arguments expected')
    root = argv[0]
    expected = open(argv[1], 'r')
    auxiliar = expected.readlines()
    expected.close()
    results = []
    for item in auxiliar:
        results.append(item[:-1])
    count = 0
    for item in Utils.listdir_files(root):
        if item in results:
            count += 1
        else:
            raise ValueError('unexpected item ---- %s -- %d' % (item, count))
    if count != len(results):
       raise ValueError('bad count ---- %d != %d' % (count, len(results)))

def test_listdir_files_matching_filenames(argv):
    """
       Test Utils.listdir_files_matching_filenames(pattern, root):
    """
    if argv == None or len(argv) != 3:
        print 'test_listdir_files_matching_filenames'
        print '  First argument root: dir for walking'
        print '  Second argument pattern: pattern for matching filenames'
        print '  Third argument expected: text file with expected results'
        raise ValueError('test_listdir_matching_filenames: three arguments expected')
    root = argv[0]
    pattern = argv[1]
    expected = open(argv[2], 'r')
    auxiliar = expected.readlines()
    expected.close()
    results = []
    for item in auxiliar:
        results.append(item[:-1])
    count = 0
    for item in Utils.listdir_files_matching_filenames(pattern, root):
        if item in results:
            count += 1
        else:
            raise ValueError('unexpected item ---- %s -- %d' % (item, count))
    if count != len(results):
       raise ValueError('bad count ---- %d != %d' % (count, len(results)))

def execute(argv):
    """
       Call this function from other modules
       execute(["no care", "rootdir", "pattern", "expected", "root", "expected", "root", "pattern", "expected"])
    """
    try:
       test_walk_matching_filenames(argv[1:4])
       print "walk_matching_filenames ok"
    except Exception, ex:
       print "walk_matching_files unexpected failure"
       print "  %s" % repr(ex)     
    try:
       test_listdir_files(argv[4:6])
       print "listdir_files ok"
    except Exception, ex:
       print "listdir_files unexpected failure"
       print "  %s" % repr(ex)     
    try:
       test_listdir_files_matching_filenames(argv[6:9])
       print "listdir_files_matching_filenames ok"
    except Exception, ex:
       print "listdir_files_matching_filenames unexpected failure"
       print "  %s" % repr(ex)     

import sys

if __name__ == "__main__":
   try:
       execute(sys.argv)
   except Exception, ex:
       print "unexpected failure"
       print "  %s" % repr(ex)
